﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class DangBaiRaoVat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string sTenDangNhap = (string)Session["TenDangNhap"];
        if (sTenDangNhap != null)
        {
            txtTenNguoiDung.Text = sTenDangNhap;
            pnlContent.Visible = true;
            pnlThongBao.Visible = false;
            
        }
        else
        {
            pnlContent.Visible = false;
            pnlThongBao.Visible = true;
            return;
        }
        LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
        if (!IsPostBack)
        {               
//             var DanhSachDanhMucChinh = from DanhMucChinh in danhsach.DANH_MUC_CHINHs where DanhMucChinh.Deleted == false select DanhMucChinh;
//             ddlDanhMucChinh.DataSource = DanhSachDanhMucChinh;
//             ddlDanhMucChinh.DataTextField = "TenDanhMucChinh";
//             ddlDanhMucCon.DataValueField = "MaDanhMucChinh";
//             ddlDanhMucChinh.DataBind();

            string maNguoiDung = (string)Session["LoaiNguoiDung"];

            //Người dùng thường
            if (maNguoiDung == "3" || maNguoiDung == "5")
            {
                var DanhSachLoaiTinRaoVat = from LoaiTinRaoVat in danhsach.LOAI_TIN_RAO_VATs  where LoaiTinRaoVat.MaLoaiTinRaoVat == 2 select LoaiTinRaoVat;
                ddlLoaiTinRaoVat.DataSource = DanhSachLoaiTinRaoVat;
                ddlLoaiTinRaoVat.DataTextField = "TenLoaiTinRaoVat";
                ddlLoaiTinRaoVat.DataValueField = "TenLoaiTinRaoVat";
                ddlLoaiTinRaoVat.DataBind();
               
            }
            else if (maNguoiDung == "4")
            {
                var DanhSachLoaiTinRaoVat = from LoaiTinRaoVat in danhsach.LOAI_TIN_RAO_VATs where LoaiTinRaoVat.MaLoaiTinRaoVat == 2 || LoaiTinRaoVat.MaLoaiTinRaoVat == 3 select LoaiTinRaoVat;
                ddlLoaiTinRaoVat.DataSource = DanhSachLoaiTinRaoVat;
                ddlLoaiTinRaoVat.DataTextField = "TenLoaiTinRaoVat";
                ddlLoaiTinRaoVat.DataValueField = "TenLoaiTinRaoVat";
                ddlLoaiTinRaoVat.DataBind();
               
            }
            else
            {
                // Nếu là admin hoặc mod
                var DanhSachLoaiTinRaoVat = from LoaiTinRaoVat in danhsach.LOAI_TIN_RAO_VATs  select LoaiTinRaoVat;
                ddlLoaiTinRaoVat.DataSource = DanhSachLoaiTinRaoVat;
                ddlLoaiTinRaoVat.DataTextField = "TenLoaiTinRaoVat";
                ddlLoaiTinRaoVat.DataValueField = "TenLoaiTinRaoVat";
                ddlLoaiTinRaoVat.DataBind();
              
            }

            
          

            var DanhSachDiaDiem = from DiaDiem in danhsach.DIA_DIEMs select DiaDiem;
            ddlDiaDiem.DataSource = DanhSachDiaDiem;
            //ddlDiaDiem.DataTextField = "TenDiaDiem";
            ddlDiaDiem.DataValueField = "TenDiaDiem";
            ddlDiaDiem.DataBind();

            if (ddlLoaiTinRaoVat.SelectedValue == "Thuong")
            {
                btnDangTinHot.Visible = false;
                btnDangTin.Visible = true;
            }
            else if (ddlLoaiTinRaoVat.SelectedValue == "Hot")
            {
                btnDangTinHot.Visible = true;
                btnDangTin.Visible = false;
            }
          
        }
// 
//         var DanhSachDanhMucChinh1 = from DanhMucChinh in danhsach.DANH_MUC_CHINHs where DanhMucChinh.Deleted == false && DanhMucChinh.TenDanhMucChinh == ddlDanhMucChinh.SelectedValue select DanhMucChinh;
// 
//         foreach (DANH_MUC_CHINH dmchinh in DanhSachDanhMucChinh1)
//         {
//             var DanhSachDanhMucCon = from DanhMucCon in danhsach.DANH_MUC_CONs where DanhMucCon.MaDanhMucChinh == dmchinh.MaDanhMucChinh select DanhMucCon;
//             ddlDanhMucCon.DataSource = DanhSachDanhMucCon;
//             ddlDanhMucCon.DataTextField = "TenDanhMucCon";
//             ddlDanhMucCon.DataValueField = "TenDanhMucCon";
//             ddlDanhMucCon.DataBind();
//         }        
    }
    protected void btnDangTin_Click(object sender, EventArgs e)
    {
        // store the file name into the string variable
        
        string fileName = MyUpload.PostedFile.FileName;

        bool flag = true;

        if (fileName != "")
        {



            // extarct and store the file extension into another variable
            string fileExtension = System.IO.Path.GetExtension(fileName).Replace(".", string.Empty).ToLower();

            // string type array having list of allowed file type extensions
            string[] validFileExtensions = new string[] { "gif", "png", "jpg" };

             flag = false;
            // loop over the array of valid file extensions to compare them with uploaded file
            foreach (string extension in validFileExtensions)
            {
                if (fileExtension == extension)
                    flag = true;
            }

        }

        // display the message based on the flag value
        if (flag == false)
        {
            lblThongBao.Text = "Vui lòng chọn file hình ảnh : gif, png, jpq";
            lblThongBao.Visible = true;
        }
        else
        {         

                string sTenDangNhap = (string)Session["TenDangNhap"];
                if (sTenDangNhap == null)
                {
                    lblThongBao.Visible = true;
                }
                else
                {
                    lblThongBao.Visible = false;

                    if (ddlLoaiTinRaoVat.SelectedValue != "Thuong")
                    {
                        LinQ_RaoVatDataContext linQ = new LinQ_RaoVatDataContext();

                        NGUOI_DUNG nd = linQ.NGUOI_DUNGs.Single(p => p.TenDangNhap == sTenDangNhap);
                        if (nd.MaLoaiNguoiDung == 4 && nd.DiemVIP >= 10 || nd.MaLoaiNguoiDung != 4)
                        {
                            nd.DiemVIP -= 10;

                            linQ.SubmitChanges();
                        }
                        else
                        {
                            Response.Write("<script>alert('Số tiền trong tài khoản không đủ để đăng tin Hot, vui lòng nạp tiền để đăng bài');</script>");
                            return;
                        }
                    }

                    SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                    string connectionString = builder.ConnectionString;
                    LinQ_RaoVatDataContext demo = new LinQ_RaoVatDataContext(connectionString);
                    TIN_RAO_VAT raovat = new TIN_RAO_VAT();

                    LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();

                    //Loại Tin Rao Vặt
                    string tenLoai = ddlLoaiTinRaoVat.Text;
                    string diaDiem = ddlDiaDiem.Text;
                    var DanhSachLoaiTinRaoVat = from LoaiTinRaoVat in danhsach.LOAI_TIN_RAO_VATs where LoaiTinRaoVat.TenLoaiTinRaoVat.ToString() == ddlLoaiTinRaoVat.Text && LoaiTinRaoVat.Deleted == false select LoaiTinRaoVat;
                    foreach (LOAI_TIN_RAO_VAT loaitrv in DanhSachLoaiTinRaoVat)
                    {
                        raovat.MaLoaiTinRaoVat = loaitrv.MaLoaiTinRaoVat;
                    }

                    //Danh Mục Con

                    var DanhSachDanhMucCon = from DanhMucCon in danhsach.DANH_MUC_CONs where DanhMucCon.TenDanhMucCon == ddlDanhMucCon.Text && DanhMucCon.Deleted == false select DanhMucCon;

                    foreach (DANH_MUC_CON dmcon in DanhSachDanhMucCon)
                    {
                        raovat.MaDanhMucCon = dmcon.MaDanhMucCon;
                    }

                    // Mã Người Dùng
                    string TenDangNhap = "";
                    if (sTenDangNhap != null)
                    {
                        TenDangNhap = sTenDangNhap;
                    }

                    var DanhSachNguoiDung = from NguoiDung in danhsach.NGUOI_DUNGs where NguoiDung.TenDangNhap == TenDangNhap select NguoiDung;
                    foreach (NGUOI_DUNG ndung in DanhSachNguoiDung)
                    {
                        raovat.MaNguoiDung = ndung.MaNguoiDung;
                    }

                    // Địa Điểm
                    var DanhSachDiaDiem = from DiaDiem in danhsach.DIA_DIEMs where DiaDiem.TenDiaDiem == ddlDiaDiem.Text select DiaDiem;
                    foreach (DIA_DIEM ddiem in DanhSachDiaDiem)
                    {
                        raovat.MaDiaDiem = ddiem.MaDiaDiem;
                    }

                    raovat.TenTinRaoVat = txtTenTinRaoVat.Text;
                    //Image

                    if (Request.Files.Count > 0)
                    {

                        if (fileName != "")
                        {

                            if (Request.Files[0].InputStream.Length > Int32.MaxValue)
                                return;
                            int dataLength = (int)Request.Files[0].InputStream.Length;
                            byte[] _byteArr = new byte[dataLength];
                            Request.Files[0].InputStream.Read(_byteArr, 0, dataLength);
                            raovat.Thumbnail = _byteArr;
                        }
                        else
                        {
                            raovat.Thumbnail = null;
                        }
                    }
                    else
                    {
                        raovat.Thumbnail = null;
                    }
                    raovat.NoiDungTinRaoVat = ckDangTinRaoVat.Content;
                    raovat.ThoiGianDang = DateTime.Now;
                    raovat.SoLanXem = 0;
                    raovat.DiemBinhChonTB = 0;
                    raovat.DaDuyet = false;
                    raovat.ViPham = false;
                    raovat.Deleted = false;
                    demo.TIN_RAO_VATs.InsertOnSubmit(raovat);
                    demo.SubmitChanges();
                    ckDangTinRaoVat.Content = "";


                    this.Response.Redirect("~/ThongBao.aspx?ThongBao=Bạn đã đăng bài rao vặt thành công ");

                }
            }
        }

    protected void ddlDanhMucChinh_SelectedIndexChanged(object sender, EventArgs e)
    {
//         LinQ_RaoVatDataContext danhsach = new LinQ_RaoVatDataContext();
//         var DanhSachDanhMucChinh = from DanhMucChinh in danhsach.DANH_MUC_CHINHs where DanhMucChinh.Deleted == false && DanhMucChinh.TenDanhMucChinh == ddlDanhMucChinh.SelectedValue select DanhMucChinh;
// 
//         foreach (DANH_MUC_CHINH dmchinh in DanhSachDanhMucChinh)
//         {
//             var DanhSachDanhMucCon = from DanhMucCon in danhsach.DANH_MUC_CONs where DanhMucCon.MaDanhMucChinh == dmchinh.MaDanhMucChinh select DanhMucCon;
//             ddlDanhMucCon.DataSource = DanhSachDanhMucCon;
//             ddlDanhMucCon.DataTextField = "TenDanhMucCon";
//             ddlDanhMucCon.DataValueField = "TenDanhMucCon";
//             ddlDanhMucCon.DataBind();
//         }        
    }   
 
    protected void ddlLoaiTinRaoVat_SelectedIndexChanged(object sender, EventArgs e)
    {
        if( ddlLoaiTinRaoVat.SelectedValue == "Thuong")
        {
            btnDangTinHot.Visible = false;
                btnDangTin.Visible = true;
        }
        else if( ddlLoaiTinRaoVat.SelectedValue == "Hot" )
        {
            btnDangTinHot.Visible = true;
            btnDangTin.Visible = false;
        }
    }
}
